/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view.component.db;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.swing.JOptionPane;
import org.opu.vcforms.actions.AbstractAction;
import org.opu.vcforms.bean.ApplicationData;
import org.opu.vcforms.bean.db.ConfigPath;
import org.opu.vcforms.io.FileIO;
import org.opu.vcforms.io.Logger;

/**
 *
 * @author yura
 */
public class OkAction extends AbstractAction {

    public static final int STORE_All = 0;
    public static final int DONT_STORE_USER = 1;
    public static final int DONT_STORE_TEMPLATE = 2;
    public static final int DONT_STORE_BOTH = 3;

    private ConnectionDialog dialog;
    private ApplicationData data;

    public OkAction(ApplicationData data) {
        this(data, null);
    }

    public OkAction(ApplicationData data, ConnectionDialog dialog) {
        this.dialog = dialog;
        this.data = data;
    }

    public void setDialog(ConnectionDialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ConnectionDialogI18n i18n = dialog.getI18n();
        List<String> msgs = new ArrayList<String>(4);
        int i = 0;

        try {
            data.setCurrentUserData(dialog.getUserData());
        } catch (ClassNotFoundException ex) {
            msgs.add(i18n.getErrorMsgUserConnectfailed());
            msgs.add(i18n.getErrorMsgClassnotfound());
            i = 1;
        } catch (SQLException ex) {
            msgs.add(i18n.getErrorMsgUserConnectfailed());
            msgs.add(i18n.getErrorMsgSql());
            i = 1;
        }

        try {
            data.setTemplateUserData(dialog.getTemplateData());
        } catch (ClassNotFoundException ex) {
            msgs.add(i18n.getErrorMsgTemplateConnectfailed());
            msgs.add(i18n.getErrorMsgClassnotfound());
            i += 2;
        } catch (SQLException ex) {
            msgs.add(i18n.getErrorMsgTemplateConnectfailed());
            msgs.add(i18n.getErrorMsgSql());
            i += 2;
        }

        switch (i) {
            case STORE_All:
                storeUser();
                storeTemplate();
                revalidateMainView();
                close();
                break;
            case DONT_STORE_USER:
                storeTemplate();
                revalidateMainView();
                break;
            case DONT_STORE_TEMPLATE:
                storeUser();
                revalidateMainView();
                break;
            case DONT_STORE_BOTH:
            default:
                break;
        }

        if (i != STORE_All) {
            err(i18n, msgs.toArray(new String[msgs.size()]));
        }
    }

    protected void revalidateMainView() {
        data.getSource().reinit();
    }

    @Override
    public String getName() {
        return "Ok";
    }

    public void storeUser() {
        boolean savePass = dialog.getUserDataPane().isSavePasswordSelected();
        File f = ConfigPath.getInstance().getUserDbProp();
        Properties p = data.getCurrentUserData().properties(savePass);
        try {
            FileIO.store(f, p, "User data properties");
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": Can't store user data:" + ex);
        }
    }

    public void storeTemplate() {
        boolean savePass = dialog.getTemplateDataPane().isSavePasswordSelected();
        File f = ConfigPath.getInstance().getTemplateDbProp();
        Properties p = data.getTemplateUserData().properties(savePass);
        try {
            FileIO.store(f, p, "Template data properties");
        } catch (IOException ex) {
            Logger.warning(getClass().getName() + ": Can't store template data:" + ex);
        }
    }

    public void close() {
        try {
            dialog.dispose();
        } catch (Exception e) {
            dialog.setVisible(false);
        }
    }

    public void err(ConnectionDialogI18n i18n, String... msg) {

        StringBuilder sb = new StringBuilder();
        for (String s : msg) {
            sb.append(s).append('\n');
        }

        try {
            JOptionPane.showMessageDialog(dialog, sb.toString(),
                    i18n.getTitle(), JOptionPane.ERROR_MESSAGE);
        } catch (HeadlessException ex) {
            Logger.warning(getClass().getName() + ":" + ex + ": " + sb);
        }
    }
}
